/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Template metadata interface
 *
 * This interface defines the methods and properties for storing and retrieving
 * template-specific data for components. It maintains a mapping from component
 * names to their associated template data.
 */
export interface TemplateMetadata {
  /**
   * The path of the file currently being processed
   */
  currentFilePath: string;

  /**
   * Map of component names to their template data
   * The key is the component name, and the value is a record of template variables
   */
  templateDataMap: Map<string, Record<string, any>>;

  /**
   * Add template data for a specific component
   * If data already exists for the component, the new data is merged with existing data
   *
   * @param {string} componentName - The name of the component
   * @param {Record<string, any>} data - The template data to add
   */
  addTemplateData(componentName: string, data: Record<string, any>): void;

  /**
   * Get template data for a specific component
   *
   * @param {string} componentName - The name of the component
   * @returns {Record<string, any> | undefined} The template data or undefined if none exists
   */
  getTemplateData(componentName: string): Record<string, any> | undefined;
}

/**
 * Implementation of the TemplateMetadata interface
 *
 * This class provides a concrete implementation for storing and retrieving
 * template data for components during the build process.
 */
export class TemplateMetadataImpl implements TemplateMetadata {
  /** The path of the file currently being processed */
  currentFilePath: string = '';

  /** Map of component names to their template data */
  templateDataMap: Map<string, Record<string, any>> = new Map();

  /**
   * Add template data for a specific component
   *
   * This method adds or merges template data for a component. If data already
   * exists for the component, the new data is merged with the existing data.
   *
   * @param {string} componentName - The name of the component
   * @param {Record<string, any>} data - The template data to add
   */
  addTemplateData(componentName: string, data: Record<string, any>): void {
    if (this.templateDataMap.has(componentName)) {
      // Merge data if the component already has template data
      const existing = this.templateDataMap.get(componentName) || {};
      this.templateDataMap.set(componentName, { ...existing, ...data });
    } else {
      // Otherwise set the data directly
      this.templateDataMap.set(componentName, data);
    }
  }

  /**
   * Get template data for a specific component
   *
   * @param {string} componentName - The name of the component
   * @returns {Record<string, any> | undefined} The template data or undefined if none exists
   */
  getTemplateData(componentName: string): Record<string, any> | undefined {
    return this.templateDataMap.get(componentName);
  }
}
